import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QPushButton, \
    QFrame, QLabel
from PyQt5 import QtCore
from service.console_service import ConsoleService

class UI_ConsoleForm(QWidget):

    def __init__(self):
        super().__init__()
        # self.setWindowTitle('嵌套布局示例')

        # 全局布局（1个）：水平
        self.whole_layout = QVBoxLayout()

        # self.setup_ui()
        self.depth = ConsoleService.get_depth() #根据机器人数量决定横向布局器数量
        self.qty = ConsoleService.get_qty()
        self.elements = self.create_array()
        self.setup_ui()
    def setup_ui(self):
        n = self.depth
        for i in range(n):
            h_layout = QHBoxLayout()
            if self.qty-(i+1)*4<0:
                m = 4+(self.qty-(i+1)*4)
            else:
                m = 4
            for j in range(m):
                frame = QFrame(self)
                frame.setStyleSheet("background-color: rgb(84, 169, 253);\n"
                "border-radius: 20px;")
                v_layout = QVBoxLayout()
                label_robot_no= QLabel("机器人编号")
                label_robot_no.setStyleSheet("color: rgb(154, 0, 0);\n"
                "font: 87 10pt \"Arial Black\";")
                label_robot_no.setAlignment(QtCore.Qt.AlignCenter)
                label_robot_type = QLabel("简历筛查机器人")
                label_robot_type.setStyleSheet("color: rgb(0, 134, 0);\n"
                "font: 87 10pt \"Arial Black\";")
                label_robot_type.setAlignment(QtCore.Qt.AlignCenter)
                label_working_time = QLabel("工作时长")
                label_working_time.setStyleSheet("color: rgb(138, 138, 69);\n"
                "font: 87 10pt \"Arial Black\";")
                label_working_time.setAlignment(QtCore.Qt.AlignCenter)
                button_robot_stop = QPushButton("停  止")
                button_robot_stop.setStyleSheet("background-color: rgb(168, 0, 0);\n"
                "color: rgb(255, 255, 255);\n"
                "border-radius: 100px;")
                # button_robot_stop.clicked.connect(lambda state,x=i,y=j: self.on_button_clicked(x,y))
                v_layout.addWidget(label_robot_no)
                v_layout.addWidget(label_robot_type)
                v_layout.addWidget(label_working_time)
                v_layout.addWidget(button_robot_stop)
                frame.setLayout(v_layout)
                h_layout.addWidget(frame)
                self.elements[i][j][0] = label_robot_no
                self.elements[i][j][1] = label_robot_type
                self.elements[i][j][2] = label_working_time
                self.elements[i][j][3] = button_robot_stop
            hwg = QWidget()
            hwg.setLayout(h_layout)
            self.whole_layout.addWidget(hwg)
        self.setLayout(self.whole_layout)
    # def on_button_clicked(self,row,col):
    #     button = self.elements[row][col][3]
    #     label_robot_no = self.elements[row][col][0]
    #     label_robot_no.setText(f'1112111-{row}{col}')
    #     button.setText(f'Clicked {row}{col}')
    #     print("点击按钮：{}{}".format(row,col))
    def create_array(self):#根据工作中机器数创建三维数据，depth根据机工作器人数量动态获得，
        cols = 4
        rows = 4
        element_array = [[[None for _ in range(cols)] for _ in range(rows)] for _ in range(self.depth)]
        return element_array

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = UI_ConsoleForm()
    win.show()
    sys.exit(app.exec_())

